import 'dart:async';

import 'load_balancer.dart';
import 'runner.dart';

class LoadBalancerEntry implements Comparable<LoadBalancerEntry> {
  int queueIndex;
  int load = 0;
  Runner runner;

  LoadBalancerEntry(this.runner, this.queueIndex);

  Future<R> run<R, P>(
      LoadBalancer balancer,
      int load,
      FutureOr<R> Function(P argument) function,
      P argument,
      Duration? timeout,
      FutureOr<R> Function()? onTimeout) {
    return runner
        .run<R, P>(function, argument, timeout: timeout, onTimeout: onTimeout)
        .whenComplete(() {
      balancer.decreaseLoad(this, load);
    });
  }

  Future close() => runner.close();

  @override
  int compareTo(LoadBalancerEntry other) {
    return load - other.load;
  }
}
